package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.entity.bo.Doctor;
import com.jrtc.base.entity.bo.Doctorservice;
import com.jrtc.base.entity.bo.Exerciseprescription;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.vo.*;
import com.jrtc.base.util.RedisUtil;
import com.jrtc.service.ExerciseprescriptionService;
import com.jrtc.service.impl.DoctorServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 运动处方表(Exerciseprescription)表控制层
 *
 * @author makejava
 * @since 2022-03-21 17:19:03
 */
@RestController
@RequestMapping("prescription")
public class ExerciseprescriptionController {
    /**
     * 服务对象
     */
    @Resource
    private ExerciseprescriptionService exerciseprescriptionService;
    @Resource
    private DoctorServiceImpl doctorservice;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Exerciseprescription selectOne(Integer id) {
        return this.exerciseprescriptionService.queryById(id);
    }
    
    @RequestMapping("getPrescriptionList")
    public ResultDTO getExerciseprescriptionList(@RequestBody Exerciseprescription exerciseprescription)
    {
        IPage<Exerciseprescription> ipage = exerciseprescriptionService.queryExerciseprescriptionList(exerciseprescription);
        return ResultDTOBuilder.success(ipage);
    }

    @RequestMapping("insertPrescription")
    public ResultDTO insertExerciseprescription(@RequestBody Exerciseprescription exerciseprescription)
    {
        Exerciseprescription tmpExerciseprescription = exerciseprescriptionService.insert(exerciseprescription);
        if (tmpExerciseprescription != null)
        {
           return ResultDTOBuilder.success("添加成功！");
        }
        return ResultDTOBuilder.failureMsg("添加失败！");
    }

    @RequestMapping("getPrescriptionById")
    public ResultDTO getExerciseprescriptionById(@RequestBody Map<String, Integer> map)
    {
        int exerciseprescriptionId = map.get("exerciseprescriptionId");
        Exerciseprescription exerciseprescription = exerciseprescriptionService.queryById(exerciseprescriptionId);
        if (exerciseprescription==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        return ResultDTOBuilder.success(exerciseprescription);
    }

    @RequestMapping("updatePrescription")
    public ResultDTO updateExerciseprescription(@RequestBody Exerciseprescription exerciseprescription)
    {
        if (exerciseprescription==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        if (exerciseprescriptionService.update(exerciseprescription)!=null)
        {
            return ResultDTOBuilder.success("修改信息成功！");
        }
        return ResultDTOBuilder.failureMsg("修改信息失败！");
    }
    
    @RequestMapping("deletePrescription")
    public ResultDTO deleteExerciseprescription(@RequestBody Map<String,Integer> map)
    {
        int exerciseprescriptionId = map.get("exerciseprescriptionId");
        if (exerciseprescriptionService.deleteById(exerciseprescriptionId))
        {
            return ResultDTOBuilder.success("删除成功！");
        }
        return ResultDTOBuilder.failureMsg("删除失败！");
    }

    /**
     * 查询指定运动处方详细信息
     * */
    @PostMapping("/prescribingDetails")
    public ResultDTO prescribingDetails(@RequestBody Map<String, Object> map){
        if (map.get("prescriptionId") != null && !map.get("prescriptionId").equals("")){
            Integer prescriptionId = Integer.parseInt(map.get("prescriptionId").toString());

            String prescriptionDesc = null;
            if (map.get("prescriptionDesc") != null && !map.get("prescriptionDesc").equals("")){
                prescriptionDesc = map.get("prescriptionDesc").toString();
            }

            Exerciseprescription exerciseprescription;
            try {
                exerciseprescription = exerciseprescriptionService.prescribingDetails(prescriptionId, prescriptionDesc);
            } catch (Exception e){
                return ResultDTOBuilder.failureMsg(e.getMessage());
            }
            if (exerciseprescription != null){
                return ResultDTOBuilder.success(exerciseprescription);
            }
            else {
                return ResultDTOBuilder.failureMsg("未查到运动处方详细信息");
            }
        }
        else {
            return ResultDTOBuilder.failureMsg("缺少必要参数");
        }
    }

    /**
     * 修改运动处方信息
     * */
    @PostMapping("/modifyInformation")
    public ResultDTO modifyInformation(@RequestBody Exerciseprescription exerciseprescription){
        if (exerciseprescription != null){
            int i;
            try {
                i = exerciseprescriptionService.modifyInformation(exerciseprescription);
            } catch (Exception e){
                return ResultDTOBuilder.failureMsg(e.getMessage());
            }
            if (i > 0){
                return ResultDTOBuilder.success("修改成功");
            }
            else {
                return ResultDTOBuilder.failureMsg("修改失败");
            }
        }
        else {
            return ResultDTOBuilder.failureMsg("缺少必要参数");
        }
    }

    /**
     * 创建运动处方
     * */
    @PostMapping("/insertExerciseprescription")
    public ResultDTO insertExerciseprescription(HttpServletRequest request, @RequestBody Exerciseprescription exerciseprescription){
        Doctor loginDoctor = null;
        try {
            loginDoctor = doctorservice.getLoginDoctor(request);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        if (loginDoctor == null){
            return ResultDTOBuilder.failureMsg("医生登录已过期，请重新登录");
        }

        int i;
        try {
            i = exerciseprescriptionService.insertExerciseprescription(exerciseprescription, loginDoctor);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }
        if (i > 0){
            return ResultDTOBuilder.success("创建成功");
        }
        else {
            return ResultDTOBuilder.failureMsg("创建失败");
        }
    }

    /**
     * 上传运动处方文件/视频
     * */
    @PostMapping("/uploadFile")
    public ResultDTO uploadFile(List<MultipartFile> file, String type){
        List<String> stringList;
        try {
            stringList = exerciseprescriptionService.uploadFile(file, type);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        return ResultDTOBuilder.success(stringList);
    }

    /**
     * 模糊搜索运动处方
     * */
    @PostMapping("/searchPrescription")
    public ResultDTO selectExerciseprescriptionByPrescriptionName(@RequestBody Map<String, Object> map){
        String prescriptionName = null;
        if (map.get("prescriptionName") != null && !map.get("prescriptionName").equals("")){
            prescriptionName = map.get("prescriptionName").toString();
        }
        List<Exerciseprescription> exerciseprescriptions = exerciseprescriptionService.selectExerciseprescriptionByPrescriptionName(prescriptionName);
        if (exerciseprescriptions != null && exerciseprescriptions.size() > 0){
            return ResultDTOBuilder.success(exerciseprescriptions);
        }
        else {
            return ResultDTOBuilder.success(null);
        }
    }

    /**
     * 条件查询康复运动
     * */
    @PostMapping("/rehabilitationExercise")
    public ResultDTO selectExerciseprescriptionByDisease(@RequestBody ParametersVo parametersVo){
        List<ExerciseprescriptionDiseaseVo> exerciseprescriptionDiseaseVoList;
        try {
            exerciseprescriptionDiseaseVoList = exerciseprescriptionService.selectExerciseprescriptionByDisease(parametersVo.getDiseaseId(), parametersVo.getPeople());
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        if (exerciseprescriptionDiseaseVoList != null && exerciseprescriptionDiseaseVoList.size() > 0){
            return ResultDTOBuilder.success(exerciseprescriptionDiseaseVoList);
        }
        else {
            return ResultDTOBuilder.failureMsg("没有查到相关数据");
        }
    }

    /**
     * 运动处方
     * */
    @PostMapping("/exercisePrescription")
    public ResultDTO exercisePrescription(@RequestBody ExercisePrescriptionVo exercisePrescriptionVo){
        List<PrescriptionTypeVo> prescriptionVo = exerciseprescriptionService.exercisePrescription(exercisePrescriptionVo);
        return ResultDTOBuilder.success(prescriptionVo);
    }

    /**
     * 查询待修改的运动处方信息
     * */
    @PostMapping("/selectExercise")
    public ResultDTO selectExercise(@RequestBody Map<String, Object> map){
        Integer id = Integer.parseInt(map.get("id").toString());
        Exerciseprescription exerciseprescription = exerciseprescriptionService.selectExercise(id);
        return ResultDTOBuilder.success(exerciseprescription);
    }
}